<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/2/24
  Time: 下午 15:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--分页导航条--%>
<div id="page_nav">
    <%--如果当前页为第一页，则不显示上一页及首页按钮--%>
    <c:if test="${requestScope.pageBook.pageNo > 1}">
        <a href="${requestScope.pageBook.url}&pageNo=1">首页</a>
        <a href="${requestScope.pageBook.url}&pageNo=${requestScope.pageBook.pageNo - 1}">上一页</a>
    </c:if>
    <%--
        分页条页码的输出：1 2 [3] 4 5，分为两种情况：
    --%>
    <c:choose>
        <%--
            一、总页码 <= 5：假设数据为20条，每页显示4条数据
               显示的页码为：1 - 总页码如：1 2 3 4 5
        --%>
        <c:when test="${requestScope.pageBook.pageTotal <= 5}">
            <c:set var="begin" value="1"/>
            <c:set var="end" value="${requestScope.pageBook.pageTotal}"/>
        </c:when>
        <%--二、总页码 > 5：假设数据为50条，每页显示4条数据--%>
        <c:when test="${requestScope.pageBook.pageTotal > 5}">
            <%--1、当前页码 <= 5，就显示1-5：1 2 3 4 5--%>
            <c:choose>
                <c:when test="${requestScope.pageBook.pageNo <= 5}">
                    <c:set var="begin" value="1"/>
                    <c:set var="end" value="5"/>
                </c:when>
                <%--2、当前页码 >= 总页码 - 2，就显示总页码-4 - 总页码：9 10 11 12 13--%>
                <c:when test="${requestScope.pageBook.pageNo >= requestScope.pageBook.pageTotal-2}">
                    <c:set var="begin" value="${requestScope.pageBook.pageTotal-4}"/>
                    <c:set var="end" value="${requestScope.pageBook.pageTotal}"/>
                </c:when>
                <%--3、其余情况如当前页在第8页则页码显示为：当前页-2 - 当前页+2：6 7 [8] 9 10--%>
                <c:otherwise>
                    <c:set var="begin" value="${requestScope.pageBook.pageNo-2}"/>
                    <c:set var="end" value="${requestScope.pageBook.pageNo+2}"/>
                </c:otherwise>
            </c:choose>
        </c:when>
    </c:choose>
    <%--分页条页码复用代码--%>
    <c:forEach var="i" begin="${begin}" end="${end}">
        <c:if test="${requestScope.pageBook.pageNo == i}">
            【${i}】
        </c:if>
        <c:if test="${requestScope.pageBook.pageNo != i}">
            <a href="${requestScope.pageBook.url}&pageNo=${i}">${i}</a>
        </c:if>
    </c:forEach>
    <%--如果当前页为最后一页，则不显示下一页及末页按钮--%>
    <c:if test="${requestScope.pageBook.pageNo < requestScope.pageBook.pageTotal}">
        <a href="${requestScope.pageBook.url}&pageNo=${requestScope.pageBook.pageNo + 1}">下一页</a>
        <a href="${requestScope.pageBook.url}&pageNo=${requestScope.pageBook.pageTotal}">末页</a>
    </c:if>
    共${requestScope.pageBook.pageTotal}页，${requestScope.pageBook.pageTotalCount}条记录
    到第<input value="${requestScope.pageBook.pageNo}" name="pn" id="pn_input"/>页<input id="jumpBut" type="button"
                                                                                      value="确定">
    <script type="text/javascript">
        $(function () {
            /*图书管理跳转到用户输入的指定页码功能*/
            $("#jumpBut").click(function () {
                let fillInPage = $("#pn_input").val();
                /*判断用户输入指定页码越界后默认跳转路径功能*/
                if (fillInPage < 1) {
                    /*href 属性是一个可读可写的字符串，可设置或返回当前显示的文档的完整 URL。*/
                    location.href = "<%=basePath%>${requestScope.pageBook.url}&pageNo=1";
                } else if (fillInPage > ${requestScope.pageBook.pageTotal}) {
                    location.href = "<%=basePath%>${requestScope.pageBook.url}&pageNo=" + ${requestScope.pageBook.pageTotal};
                } else {
                    location.href = "<%=basePath%>${requestScope.pageBook.url}&pageNo=" + fillInPage;
                }
            })
        })
    </script>
</div>
